#include <cstdio>

const int Limit_Size = 100 + 10;

bool hide[ Limit_Size ][ Limit_Size ], check[ Limit_Size * 2 ];
int match[ Limit_Size * 2 ], n, m;

bool dfs( int p )
{
    int i, t;
    for ( i = 0; i < m; i++ )
        if ( hide[ i ][ p ] && !check[ i ] )
        {
            check[ i ] = true;
            t = match[ i ];
            match[ i ] = p;
            if ( t == -1 || dfs( t ) )
                return true;
            match[ i ] = t;
        }
    return false;
}

int main( )
{
    int k, i, j, tmp, a, b, count;
    while ( scanf("%d", &n) != EOF && n )
    {
        scanf("%d%d", &m, &k);
        for ( i = 0; i < n; i++ )
            for ( j = 0; j < m; j++ )
                hide[ i ][ j ] = false;
        for ( i = 0; i < n + m; i++ )
            match[ i ] = -1;
        while ( k-- )
        {
            scanf("%d%d%d", &tmp, &a, &b);
            if ( a != 0 && b != 0 )
                hide[ a ][ b ] = true;
        }
        count = 0;
        for ( i = 0; i < n; i++ )
        {
            for ( j = 0; j < m; j++ )
                check[ j ] = false;
            if ( dfs( i ) )
                count++;
        }
        printf("%d\n", count);
    }
    return 0;
}
